import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)


const state = {
  token:'',
  notes: [],
  activeNote: {},
	latitude: '23.5553', // 当前位置纬度
	longitude: '', // 当前位置经度
	userInfo: null, //用户信息
	shopid: null, // 商铺id
	remarkText: null,//可选择的下单备注
	inputText: '',//手动输入下单备注
	invoice: false,//是否需要开发票，默认否
	searchAddress: null,//添加地址时选择的地址，通过vuex传递给上一级页面
	geohash: null,//在msite页面中存入geohash，这个全局的变量在任何地方都有可能会被用到
	choosedAddress: null,//下单页面确认选择的收货地址
	addressIndex: null,//收获地址的索引值
	needValidation: null,//下单时需要输入验证码的返回值
	cartId: null,//订单的id
	sig: null,//订单的sig
	orderParam: null,//传递给后台的订单参数
	orderMessage: null,//下单成功，保存订单返回信息
	orderDetail: null,//进入订单详情页前保存该订单信息
	defaultCanteen:'',//当前默认食堂
	canteen:{},//当前校区食堂列表
	login: true,//登陆的状态，默认登陆
	numcom: null,//购物车购买总份数
	comtotal:null,//首页购物车未打折总价
	foodClass:null,//单个食堂菜品分类(首页左右滑动栏)
	buylist:[],//加入购物车的商品列表(含每种食品详细信息)
	selectedFoods:null,//添加到购物车的商品列表
  pullGoods:null,
  defaultCampousId:null,
  defaultCampousName:null,
  currentShop:null,//当前窗口下标
  currentName:null,//当前窗口名称
	fee:0,//默认配送价格
  mark:null,//备注
}

const mutations = {
  ADD_NOTE (state) {
    const newNote = {
      text: 'New note',
      favorite: false
    }
    state.notes.push(newNote)
    state.activeNote = newNote
  },

  ADD_FOOD (state,payload) {
    const newFood = {
      id: payload.id,
      num: payload.num,
      name:payload.name,
      price:payload.price,
      total:payload.num*payload.price
    }
    state.buylist.push(newFood);
  },

  DELETE_NOTE (state) {
    state.notes.$remove(state.activeNote)
    state.activeNote = state.notes[0]
  },

  TOGGLE_FAVORITE (state) {
    state.activeNote.favorite = !state.activeNote.favorite
  },

  NUM_COUNT (state,payload){
    payload.num>0?state.numcom++:state.numcom--;
  },

  SELECTED_FOODS (state,payload) {
  	state.selectedFoods = payload
  },

  PULL_GOODS (state,payload) {
    state.pullGoods = payload
  },

  DEFAULT_CAMPOUS_ID (state,payload) {
    state.defaultCampousId = payload
  },

  DEFAULT_CAMPOUS_NAME (state,payload) {
    state.defaultCampousName = payload
  },

  CURRENT_SHOP (state,payload) {
    state.currentShop = payload
  },

  CURRENT_NAME (state,payload) {
    state.currentName = payload
  },

  MARK_BUY (state,payload) {
    state.mark = payload
  },

  COM_TOTAL (state,payload) {
  	state.comtotal =null;
  	payload.forEach(function(food){
  		state.comtotal += food.price*food.count
  	})
  }

}



export default new Vuex.Store({
  state,
  mutations
})